package com.rdc.example.service.impl;

import com.rdc.example.bean.Result;
import com.rdc.example.bean.dto.FollowDTO;
import com.rdc.example.bean.dto.UserDTO;
import com.rdc.example.dao.FollowDao;
import com.rdc.example.dao.impl.FollowDaoImpl;
import com.rdc.example.dao.impl.UserDaoImpl;
import com.rdc.example.entity.Follow;
import com.rdc.example.entity.User;
import com.rdc.example.service.FollowService;
import com.rdc.example.service.UserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author duck
 */
public class FollowServiceImpl implements FollowService {


    public static void main(String[] args) {
        FollowService followService = new FollowServiceImpl();
        FollowDTO followDTO = new FollowDTO();
        List<UserDTO> list= followService.listUserDTOByFollowerId(1);
        System.out.println(list );
    }
    /**
     *
     * @param followDTO
     * @return
     */
    @Override
    public Result insertFollow(FollowDTO followDTO) {
        try {
            FollowDao followDao = new FollowDaoImpl();
            Follow followEntity = convertToEntity(followDTO);
            boolean register = followDao.insert(followEntity);
            return register ? Result.success("关注成功", null)
                    : Result.fail("关注失败", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     *
     * @param followId
     * @return
     */
    @Override
    public Result deleteFollowById(Integer followId) {
        try {
            FollowDao followDao = new FollowDaoImpl();
            boolean register = followDao.deleteById(followId);
            return register ? Result.success("取关成功", null)
                    : Result.fail("取关失败", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @param followDTO
     * @return
     */
    @Override
    public Result turnFollow(FollowDTO followDTO) {
        try {
            FollowDao followDao = new FollowDaoImpl();

            if(followDao.followCheck(followDTO.getFollowerId(), followDTO.getFollowedId())) {
                //关注了
                return deleteFollowByFollowerIdFollowedId(followDTO.getFollowerId(), followDTO.getFollowedId());
            }else{
                //未关注
                return insertFollow(followDTO);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Result deleteFollowByFollowerIdFollowedId(Integer followerId, Integer followedId) {
        try {
            FollowDao followDao = new FollowDaoImpl();
            boolean register = followDao.deleteByFollowerIdFollowedId(followerId, followedId);
            return register ? Result.success("取关成功", null)
                    : Result.fail("取关失败", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @param followerId
     * @return
     */
    @Override
    public List<UserDTO> listUserDTOByFollowerId(Integer followerId) {
        try {//List<UserDTO> getUserDTOListByIds(List<Integer> userIds) {
            FollowDao followDao = new FollowDaoImpl();
            List<Integer> followedIds = followDao.listFollowedIdsByFollowerId(followerId);
            if(followedIds.isEmpty()){
                return null;
            }
            UserService userService = new UserServiceImpl();
            return userService.getUserDTOListByIds(followedIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private List<FollowDTO> convertAndEnrichFollow(List<Follow> followEntityList) {
        List<FollowDTO> dtos = new ArrayList<>();
        Set<Integer> userIds = followEntityList.stream()
                .map(Follow::getFollowedId)
                .collect(Collectors.toSet());
        UserDaoImpl userDao = new UserDaoImpl();
        Map<Integer, User> userMap = userDao.getUsersByIds(userIds);
        // 转换并填充被followedName
        for (Follow entity : followEntityList) {
            FollowDTO dto = convertToDTO(entity);
            User user = userMap.get(entity.getFollowedId());
            if (user != null) {
                dto.setFollowedName(user.getUserName());
            } else {
                dto.setFollowedName("未知");
                System.err.println("用户不存在: userId=" + entity.getFollowedId());
            }
            dtos.add(dto);
        }
        return dtos;
    }






    private Follow convertToEntity(FollowDTO followDTO) {
        Follow follow = new Follow();
        follow.setId(followDTO.getId());
        follow.setFollowerId(followDTO.getFollowerId());
        follow.setFollowedId(followDTO.getFollowedId());
        follow.setCreatedAt(followDTO.getCreatedAt());
        return follow;
    }
    private FollowDTO convertToDTO(Follow follow) {
        FollowDTO followDTO = new FollowDTO();
        followDTO.setId(follow.getId());
        followDTO.setFollowerId(follow.getFollowerId());
        followDTO.setFollowedId(follow.getFollowedId());
        followDTO.setCreatedAt(follow.getCreatedAt());
        return followDTO;
    }


}
